import { Directive, Type, Component } from '@angular/core';
import { DirectiveResolver } from '@angular/compiler';
import { Platform } from './env';



export class MyViewUrlResolver extends DirectiveResolver {
    resolve(type: Type<any>, throwIfNotFound?: boolean): Directive {
        const view = <any>super.resolve(type, throwIfNotFound);
        if (typeof view['template'] !== 'undefined') {
            console.log(view['template']);
            const originalTemplate = (<Component>view).template;
            (<Component>view).template = relacePaltform(originalTemplate);
        }
        console.log(view);
        return view;
    }
}

function relacePaltform(tmp: string) {
    return tmp.replace('{{platform}}', Platform);
}


function PlatformComponentUrl(url: string): string {
    console.log(url);
    let platformTemplateUrl = '';
    if (url) {
        const templateUrl = url;
        const pointHtml = templateUrl.lastIndexOf('.');
        platformTemplateUrl = templateUrl.substring(0, pointHtml)
            + '.'
            + Platform
            + templateUrl.substring(pointHtml);
    }

    return platformTemplateUrl;
}
